
#include "MessageBrowser.hpp"
#include "../OLApp/Application.hpp"

#include <QDateTime>
#include <QHeaderView>

namespace NSWidgets {

	MessageBrowser::MessageBrowser(QWidget* w) :
		NSCore::AbstractTableWidget(w)
	{
		setShowGrid(false);
		setColumnCount(3);
		horizontalHeader()->setVisible(false);
		horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
		horizontalHeader()->setStretchLastSection(true);
		setSelectionMode(QAbstractItemView::SingleSelection);
		setSelectionBehavior(QAbstractItemView::SelectRows);
	}


	MessageBrowser::~MessageBrowser() {


	}

	void MessageBrowser::putLogText(QString logtext, int type) {
		/*0-info, 1-warning, 2-error*/
		int set_lines = rowCount() + 1;
		setRowCount(set_lines);
		setRowHeight(set_lines - 1, _S(24));
		QString current = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
		QTableWidgetItem* item_time = new QTableWidgetItem(current);
		QTableWidgetItem* item_type = new QTableWidgetItem;
		QTableWidgetItem* message = new QTableWidgetItem(logtext);

		setItem(set_lines - 1, 0, item_time);
		setItem(set_lines - 1, 1, item_type);
		setItem(set_lines - 1, 2, message);

		item_time->setFlags(item_time->flags() & ~Qt::ItemIsEditable);
		item_type->setFlags(item_type->flags() & ~Qt::ItemIsEditable);
		message->setFlags(message->flags() & ~Qt::ItemIsEditable);
		
		switch (type) {
		case 0:
			item_type->setText("INFO");
			item_type->setForeground(QColor("#000000"));
			message->setForeground(QColor("#000000"));
			item_time->setIcon(QIcon(":/Res/icon_message_info.png"));
			break;
		case 1:
			item_type->setText("WARNING");
			item_type->setForeground(QColor("#FFD700"));
			message->setForeground(QColor("#FFD700"));
			item_time->setIcon(QIcon(":/Res/icon_message_warning.png"));
			break;
		case 2:
			item_type->setText("ERROR");
			item_type->setForeground(QColor("#FF0000"));
			message->setForeground(QColor("#FF0000"));
			item_time->setIcon(QIcon(":/Res/icon_message_error.png"));
			break;
		default:
			break;
		}
	}
}
